package service

import (
	"admin/app/model"
	"admin/tools"
)

/*
* 保存笔记
 */
func SaveNote(note model.Note) (bool, error) {

	result := tools.DB.Save(&note)

	if result.Error != nil {
		return false, result.Error
	}
	return true, nil
}

// DeleteNote 删除笔记
// @param id 笔记id
func DeleteNote(id string) (bool, error) {
	var note model.Note
	result := tools.DB.Where("id = ?", id).Delete(&note)

	if result.Error != nil {
		return false, result.Error
	}
	return true, nil
}

// UpdateNote 编辑笔记
// @param id 笔记id
func UpdateNote(id string) (bool, error) {
	var note model.Note
	result := tools.DB.Where("id = ?", id).Delete(&note)

	if result.Error != nil {
		return false, result.Error
	}
	return true, nil
}

// ReadNote 读取笔记内容
func ReadNote(id string) (model.Note, error) {
	var note model.Note
	result := tools.DB.First(&note, "id = ?", id)

	if result.Error != nil {
		return note, result.Error
	}
	return note, nil
}

// ListNote 笔记列表
func ListNote(page int, pageSize int) ([]model.Note, int64) {
	var total int64
	var noteList []model.Note
	var note = model.Note{}

	offset := (page - 1) * pageSize
	tools.DB.Limit(pageSize).Offset(offset).Find(&noteList)
	tools.DB.Model(&note).Count(&total)
	return noteList, total
}
